<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/16
 * Time: 14:43
 */
namespace App\Listeners;

use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Events\OrderFinish;
use App\Http\Model\Mysql\ProductCommissionModel;
use App\Http\Model\Mysql\UserModel;
use App\Http\Model\Mysql\OrderIncomeModel;
use App\Http\Model\MongoDB\DistributionConfig;

class OrderFinishListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event
     *
     * @param OrderFinish $event
     * @return bool
     */
    public function handle(OrderFinish $event)
    {
//        \Log::info($event->order_model);

        /**
         * 查看购买用户的信息
         */
        $user_info  =   UserModel::where(['uid' => $event->order_model->user_id])->select(['uid', 'nickname', 'phone', 's_uid', 't_uid'])->first();
        if (empty($user_info)) {
            return false;
        }

        /**
         * 查看店铺佣金配置
         */
        $shop_config    =   DistributionConfig::where(['companyId' => '621b9cc5-11b3-4d0e-ad82-981351e25336'])->select(['distriRate', 'liveBoxDistriLevel', 'commissionCalculate'])->first();
        if (empty($shop_config) || $shop_config['commissionCalculate']['lockStartPoint'] == 0) {
            return false;
        }

        /**
         * 用户分佣
         */
        $share_price    =   $invite_price   =   $box_price  =   0.00;
        foreach ($event->order_model->product as $item) {
            //查看每个产品的分佣金额
            $commission_info    =   ProductCommissionModel::where(['product_id' => $item->product_id, 'is_delete' => ProductCommissionModel::NOT_DELETE])->select(['id', 'product_id', 'commission_price', 'commission_rate', 'invite_rate', 'self_rate', 'is_commission', 'is_box_commission', 'is_default', 'is_box_default', 'first_rate', 'second_rate', 'third_rate'])->first();
            if (empty($commission_info)) {
                continue;
            }

            /**
             * 佣金结算方式
             */
            if ($shop_config['commissionCalculate']['method'] == 0) {       //售价结算
                $single_calc    =   $item->product_total;
            } else {            //盈利结算
                $single_calc    =   $item->product_count * abs($item->good->sell_price - $item->good->cost_price);
            }

            //购买分佣
            if ($commission_info->is_commission == ProductCommissionModel::IS_COMMISSION) {
                /**
                 * 是否默认分佣
                 */
                if ($commission_info->is_default == ProductCommissionModel::IS_DEFAULT) {
                    $share_price    +=  $single_calc * ($shop_config['distriRate']['commissionRate'] / 100);
                    $invite_price   +=  $single_calc * ($shop_config['distriRate']['inviteCommissionRate'] / 100);
                } else {
                    $share_price    +=  $single_calc * ($commission_info->commission_rate / 100);
                    $invite_price   +=  $single_calc * ($commission_info->invite_rate / 100);
                }
            }

            /**
             * 盒子分佣
             */
            if ($commission_info->is_box_commission == ProductCommissionModel::IS_COMMISSION) {

            }
        }

        if ($user_info['s_uid'] != 0) {
            //上级代理的用户信息
            $share_info =   UserModel::where(['uid' => $user_info['s_uid']])->select(['uid', 'nickname', 'phone', 's_uid', 't_uid', 'balance', 'total_balance'])->first();
            if (!empty($share_info)) {
                //一级分佣
                $user_income['oid']    =    $event->order_model->id;
                $user_income['uid']    =    $share_info['uid'];
                $user_income['shop_id']=    1;
                $user_income['reward'] =    OrderIncomeModel::REWARD_PROMOTE;
                $user_income['type']   =    1;
                $user_income['status'] =    OrderIncomeModel::STATUS_LOCk;
                $user_income['before'] =    $share_info['balance'];
                $user_income['after']  =    $share_info['balance'] + $share_price;
                $user_income['income'] =    $share_price;
                $user_income['day_time']=   Carbon::now()->toDateTimeString();
                $user_income['created_at']  =   $user_income['updated_at']  =   Carbon::now()->timestamp;
                $total_income[]        =    $user_income;

                if ($user_info['t_uid'] != 0) {
                    //二级分佣
                    $second_user    =   UserModel::where(['uid' => $user_info['t_uid']])->select(['uid', 'nickname', 'phone', 's_uid', 't_uid', 'balance', 'total_balance'])->first();
                    if (!empty($second_user)) {
                        $user_income['oid']    =    $event->order_model->id;
                        $user_income['uid']    =    $second_user['uid'];
                        $user_income['shop_id']=    1;
                        $user_income['reward'] =    OrderIncomeModel::REWARD_PROMOTE;
                        $user_income['type']   =    2;
                        $user_income['status'] =    OrderIncomeModel::STATUS_LOCk;
                        $user_income['before'] =    $second_user['balance'];
                        $user_income['after']  =    $second_user['balance'] + $share_price * (20 / 100);
                        $user_income['income'] =    $share_price * (20 / 100);
                        $user_income['day_time']=   Carbon::now()->toDateTimeString();
                        $user_income['created_at']  =   $user_income['updated_at']  =   Carbon::now()->timestamp;
                        $total_income[]        =    $user_income;
                    }
                }
            }
        }

        if ($event->order_model->share_id != 0) {
            //查看分享者的用户信息
            $invite_info    =   UserModel::where(['uid' => $event->order_model->share_id])->select(['uid', 'nickname', 'phone', 's_uid', 't_uid', 'balance', 'total_balance'])->first();
            if (!empty($invite_info)) {
                //邀请奖励
                $user_income['oid']    =    $event->order_model->id;
                $user_income['uid']    =    $event->order_model->share_id;
                $user_income['shop_id']=    1;
                $user_income['reward'] =    OrderIncomeModel::REWARD_INVITE;
                $user_income['type']   =    1;
                $user_income['status'] =    OrderIncomeModel::STATUS_LOCk;
                $user_income['before'] =    $invite_info['balance'];
                $user_income['after']  =    $invite_info['balance'] + $invite_price;
                $user_income['income'] =    $invite_price;
                $user_income['day_time']=   Carbon::now()->toDateTimeString();
                $user_income['created_at']  =   $user_income['updated_at']  =   Carbon::now()->timestamp;
                $total_income[]        =    $user_income;
            }
        }

        if (!empty($total_income)) {
            OrderIncomeModel::insert($total_income);
        }

    }
}
